<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Hash;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use PDO;
use Mail;
use Gregwar\Captcha\CaptchaBuilder;
class HomeDengluController extends Controller
{
    public function getIndex()
    {
    	  // echo '111111';
    	//解析模板
    	 return view('home.login.user');
    }

    public function postInsert(Request $request)
    {
        // echo 4444444444;
         // dd($request->all());
        // $data = $request->only(['username','password']);
        $username = $request->input('username');

       // $request->input('password');

        $res = DB::table('users')->where('username',$username)->first();
        // dd($res);
         // dd($res->password);
        // $pass = DB::table('users')->where('password')->select();
        // dd($pass);
        if(!empty($res)){
            // dd($res->password);
            if(Hash::check($request->input('password'),$res->password)){
                session(['id' =>$res->id]);
                return redirect('/home/goods/index')->with('id','欢迎'.$username.'登录');
                // echo 1222222;
            }else{ 
                return back()->with('error','用户名或密码不正确');
            }

            // echo 111111;
        }else{
            return back()->with('error','用户名或密码不正确');
        }



    }   

    public function getYzm(Request $request)
    {
        // dd($request->all());
        //检测验证码是否正确
        if($request->input('name') != session('Vcode')){
            echo 0;
        }else{
            echo 1;
        }
    }


    public function getDelete()
    {
        // echo 1111111;

        session_start('id');
        $_SESSION = NULL;

        if($_SESSION == NULL)
        {
             return redirect('/home/index')->with('success','注销成功');
        }
    }

    public function getWang()
    {
        // echo 222222222;
        return view('home.login.zhao');
    }

    public function postWxr(Request $request){
        // dd($request->all());
         // $data = $request->only(['username','email']);


         $username = $request->input('username');
         
         $email = $request->input('email');

            // 执行注册
         $id = DB::table('users')->where('username',$username)->where('email',$email)->first();
             // dd($id);
         if($id){
            // if($request->input('token') == $id->token){}
            //注册成功  发送激活邮件
        // echo 11111111;
            $this->seedmail($id->id,$request->input('email'),$id->token);
            return view('home.login.success')->with('success','欢迎登陆火星网,火星不一样的世界');


         }else{
            return back()->with('error','用户名,邮箱不正确');
         }
    }
    
    public function seedmail($id,$email,$token)
        {

          // $id =5181;$email='18735269417@163.com';
            //第二种
           // 发送其它形式的邮件  可以发送模板
         Mail::send('home.login.dengemail', ['id'=>$id,'email'=>$email,'token'=>$token], function ($message)use($email) {
             $message->to($email)->subject('注册账号激活邮件');
             });
            // return view('home.login.email');
            
        }

        public function getJihuo(Request $request)
        {
            // echo '激活';
             $id= $request->input('id');

            //查询用户
            $user = DB::table('users')->where('id',$id)->first();

            if($user){
                if($request->input('token') == $user->token){
                    $arr = ['status'=>2,'token'=>str_random(50)];
                    if(DB::table('users')->where('id',$id)->update($arr)){
                        // echo '激活成功';
                        return view('home.login.zhuxin',['users'=>$user]);
                    }else{
                        return redirect('home.login.zhuemail');
                    }
                }else{
                    return redirect('home.login.zhuemail');
                }
            }else{
                echo '没有查询到您的信息';
            }
        }

         public function getVcode()
        {
         ob_clean();//清除输出缓存区的内容
        //生成验证码图片的Builder对象，配置相应属性
        $builder = new CaptchaBuilder;
        //可以设置图片宽高及字体
        $builder->build($width = 100, $height = 40, $font = null);
        //获取验证码的内容
        $phrase = $builder->getPhrase();

        //把内容存入session
        session(['Vcode'=>$phrase]);
        //生成图片
        header("Cache   -Control: no-cache, must-revalidate");
        header('Content-Type: image/jpeg');
        $builder->output();
        // die();
        }


        public function  postXin(Request $request)
        {
            // dd($request->all());
              $data = $request->only(['password']);
             // $data = $request->input('password');
             $data['password'] = Hash::make($data['password']);
             $data['token'] = str_random(50);

           
            $id= $request->input('id');


            // 执行注册
             $users = DB::table('users')->where('id',$id)->update($data); 

             if($users){
                return view('home.login.user');
             }

        }

}
